﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using RhythmComet.Utils;

namespace RhythmComet.Gameplay
{
    public enum Shape
    {
        Triangle,
        Square,
        Hexagon,
        Diamond
    }

    public class Polygon
    {
        #region Fields and Properties
        Vector3[] vertices;

        public Vector3[] Points
        {
            get { return vertices; }
        }
        #endregion

        public Polygon(Vector3[] vertices)
        {
            this.vertices = vertices;
        }

        #region static methods
        public static Polygon CreateTriangle(float size)
        {
            Vector3[] points = new Vector3[3];
            points[0] = (new Vector3(-0.5f, -0.433f, 0.0f)) * size;
            points[1] = (new Vector3(0.0f, 0.433f, 0.0f)) * size;
            points[2] = (new Vector3(0.5f, -0.433f, 0.0f)) * size;

            return new Polygon(points);
        }

        public static Polygon CreateSquare(float size)
        {
            Vector3[] points = new Vector3[4];

            points[0] = (new Vector3(-0.5f, -0.5f, 0.0f)) * size;
            points[1] = (new Vector3(-0.5f, 0.5f, 0.0f)) * size;
            points[2] = (new Vector3(0.5f, 0.5f, 0.0f)) * size;
            points[3] = (new Vector3(0.5f, -0.5f, 0.0f)) * size;

            return new Polygon(points);
        }

        public static Polygon CreateHexagon(float size)
        {
            Vector3[] points = new Vector3[6];

            points[0] = (new Vector3(0.5f, -0.866f, 0.0f)) * size / 1.8f;
            points[1] = (new Vector3(-0.5f, -0.866f, 0.0f)) * size / 1.8f;
            points[2] = (new Vector3(-1.0f, 0.0f, 0.0f)) * size / 1.8f;
            points[3] = (new Vector3(-0.5f, 0.866f, 0.0f)) * size / 1.8f;
            points[4] = (new Vector3(0.5f, 0.866f, 0.0f)) * size / 1.8f;
            points[5] = (new Vector3(1.0f, 0.0f, 0.0f)) * size / 1.8f;

            return new Polygon(points);
        }

        public static Polygon CreateDiamond(float size)
        {
            Vector3[] points = new Vector3[4];

            points[0] = (new Vector3(-0.5f, 0.0f, 0.0f)) * size / 1.4f;
            points[1] = (new Vector3(0.0f, 0.866f, 0.0f)) * size / 1.4f;
            points[2] = (new Vector3(0.5f, 0.0f, 0.0f)) * size / 1.4f;
            points[3] = (new Vector3(0.0f, -0.866f, 0.0f)) * size / 1.4f;

            return new Polygon(points);
        }

        public bool Contains(Vector3 point)
        {
            bool oddNodes = false;

            int j = vertices.Length - 1;
            float x = point.X;
            float y = point.Y;

            for (int i = 0; i < Points.Length; i++)
            {
                Vector3 tpi = Points[i];
                Vector3 tpj = Points[j];

                if (tpi.Y < y && tpj.Y >= y || tpj.Y < y && tpi.Y >= y)
                    if (tpi.X + (y - tpi.Y) / (tpj.Y - tpi.Y) * (tpj.X - tpi.X) < x)
                        oddNodes = !oddNodes;

                j = i;
            }

            return oddNodes;
        }
        #endregion
    }
}
